﻿using AutoMapper;

using Microsoft.IdentityModel.Tokens;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using ZFB.Common;
using ZFB.DTO;
using ZFB.IServices;
using ZFB.Repository.Setting;

namespace ZFB.Services
{
    public class SettingServices : ISettingServices
    {
        public readonly ISettingRepository  settingRepository;
        public readonly IMapper mapper;
        public SettingServices(ISettingRepository settingRepository, IMapper mapper)
        {
            this.settingRepository = settingRepository;
            this.mapper = mapper;
        }

        public async Task<ApiResult> GetAllAsnyc()
        {
            var data = await settingRepository.GetListAsync();
            if (data.IsNullOrEmpty()) return new("获取失败!");
            var dato=mapper.Map<List<SettingDTO>>(data);
            return new(dato);
        }

        public async Task<ApiResult> GetBoolValueAsnyc(string name)
        {
            var data = await settingRepository.GetValueAsync(name);
            return data == null ? new("配置不存在!") : new(Convert.ToBoolean(data));
        }

        public async Task<ApiResult> GetlntValueAsnyc(string name)
        {
            var data = await settingRepository.GetValueAsync(name);
            return data == null? new("配置不存在!"): new(Convert.ToInt32( data));
        }

        public async Task<ApiResult> GetValueAsnyc(string name)
        {
            var value = await settingRepository.GetValueAsync(name);
            return value == null ? new("配置不存在!") : new(data:value);
        }

        public async Task<ApiResult> SetValueAsnyc(string name, bool value)
        {
            var data = await settingRepository.GetModelAsync(e => e.Name == name);
            if (data == null)
            {
                var res = await settingRepository.SetValueAsync(name, value.ToString());
                return res <= 0 ? new ApiResult("添加失败！") : new ApiResult();
            }
            else
            {
                data.Value = value.ToString();
                var res = await settingRepository.UpdateAsync(data);
                return res <= 0 ? new ApiResult("更新失败！") : new ApiResult();
            }
        }

        public async Task<ApiResult> SetValueAsnyc(string name, int value)
        {

            var data = await settingRepository.GetModelAsync(e => e.Name == name);
            if (data == null)
            {
              var res=  await settingRepository.SetValueAsync(name, value.ToString());
                return res <= 0 ? new ApiResult("添加失败！") : new ApiResult();
            }
            else
            {
                data.Value = value.ToString();
              var res=  await settingRepository.UpdateAsync(data);
                return res <= 0 ? new ApiResult("更新失败！") : new ApiResult();
            }
            
        }

        public async Task<ApiResult> SetValueAsnyc(string name, string value)
        {
            var data = await settingRepository.GetModelAsync(e => e.Name == name);
            if (data == null)
            {
                var res = await settingRepository.SetValueAsync(name, value);
                return res <= 0 ? new ApiResult("添加失败！") : new ApiResult();
            }
            else
            {
                data.Value = value;
                var res = await settingRepository.UpdateAsync(data);
                return res <= 0 ? new ApiResult("更新失败！") : new ApiResult();
            }
        }
    }
}
